<script setup lang="ts">
import { RouterView } from 'vue-router'
import ToolBar from './components/ToolBar/index.vue'
import { ref, onMounted } from 'vue'

// 使用 ref 来存储响应数据
const appData = ref<any>(null)

// 在组件挂载时获取数据
onMounted(async () => {

  const response = await window.electron.ipcRenderer.invoke('getData')

  console.log(response);
  
  appData.value = response

  // 检查更新
  // 当前版本
  const currentVersion = 1.0
  console.log(response.version);
  
  if(response.version > currentVersion){
    const isConfirmed = window.confirm("有新版本，去下载更新吗？");
    if (isConfirmed) {
      window.open(response.versionUrl); // 新标签页打开
    }
  }

  // 读取缓存数据
  const cacheData = JSON.parse(localStorage.getItem('cacheData') || '[]')

  // 遍历 gameList，检查是否在缓存中
  appData.value.gameList = appData.value.gameList.map(game => {
    // 检查当前游戏是否在缓存中（匹配 gameName）
    const isInstalled = cacheData.some(
      cachedGame => cachedGame.game === game.gameName
    )

    // 返回新对象，添加 install 字段
    return {
      ...game,
      install: isInstalled, // true 表示已安装，false 表示未安装
    }
  
  })

  // 自动读取steam库
  const currentSteamPath = localStorage.getItem('steamPath')
  if(!currentSteamPath){
    const steamPath = await window.electron.ipcRenderer.invoke('getSteamPath')
    if(steamPath!=null){
      localStorage.setItem('steamPath',steamPath)
    }
  }

  
})
</script>

<template>
  <ToolBar></ToolBar> 
  <RouterView :data="appData"></RouterView>
</template>